#include "CPanelExtraTools.h"

#include <QFileDialog>
#include <QGridLayout>
#include <QPushButton>

CPanelExtraTools::CPanelExtraTools(QWidget *parent) :
    CPanelWidget(parent),
    btn_config(new QPushButton(this)),
    btn_export(new QPushButton(this)),
    btn_import(new QPushButton(this))
{
    // layout
    auto lay = new QGridLayout;
    lay->addWidget(btn_config, 0, 0);
    lay->addWidget(btn_export, 1, 0);
    lay->addWidget(btn_import, 1, 1);
    getContent()->setLayout(lay);
    getHeaderButton(0)->hide();
    getHeaderButton(1)->hide();
    setWindowTitle(tr("Extra Tools"));

    // properaties
    btn_config->setText(tr("Config"));
    btn_export->setText(tr("Export"));
    btn_import->setText(tr("Import"));

    // connect
    connect(btn_config, &QPushButton::clicked, this, &CPanelExtraTools::on_btn_config_clicked);
    connect(btn_export, &QPushButton::clicked, this, &CPanelExtraTools::on_btn_export_clicked);
    connect(btn_import, &QPushButton::clicked, this, &CPanelExtraTools::on_btn_import_clicked);
}

CPanelExtraTools::~CPanelExtraTools()
{
}

void CPanelExtraTools::on_btn_config_clicked()
{
    emit configDevices();
}

void CPanelExtraTools::on_btn_export_clicked()
{
    auto filename = QFileDialog::getSaveFileName(this, tr("export"), "./config", "XML files (*.xml)");
    if (filename.isEmpty())
    {
        return;
    }

    emit exportConfigFile(filename);
}

void CPanelExtraTools::on_btn_import_clicked()
{
    auto filename = QFileDialog::getOpenFileName(this, tr("import"), "./config", "XML files (*.xml)");
    if (filename.isEmpty())
    {
        return;
    }

    emit importConfigFile(filename);
}